package com.koron.order.experiment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.experiment.bean.po.ExperimentEquipmentSignPo;
import com.koron.order.experiment.mapper.ExperimentEquipmentSignMapper;
import com.koron.order.experiment.service.ExperimentEquipmentSignService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * (ExperimentEquipmentSign)表服务实现类
 *
 * @author zhouj
 * @since 2021-11-08 17:51:49
 */
@Service("experimentEquipmentSignService")
public class ExperimentEquipmentSignServiceImpl implements ExperimentEquipmentSignService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public String insertOrUpdate(SessionFactory factory, ExperimentEquipmentSignPo experimentEquipmentSign) {

        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);

        int i = mapper.insertOrUpdate(experimentEquipmentSign);

        return experimentEquipmentSign.getId();
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<ExperimentEquipmentSignPo> experimentEquipmentSignList) {

        if (CollectionUtils.isEmpty(experimentEquipmentSignList)) {
            return 0;
        }

        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);
        experimentEquipmentSignList.forEach(p -> {
            if (Objects.isNull(p.getSign())) {
                p.setSign(0);
            }
        });
        mapper.deleteByExperimentEquipmentId(experimentEquipmentSignList.get(0).getExperimentEquipmentId());
        return mapper.insertOrUpdateBatch(experimentEquipmentSignList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, ExperimentEquipmentSignPo experimentEquipmentSign) {

        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);

        return mapper.insert(experimentEquipmentSign);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<ExperimentEquipmentSignPo> experimentEquipmentSignList) {

        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);

        return mapper.insertBatch(experimentEquipmentSignList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, ExperimentEquipmentSignPo experimentEquipmentSign) {
        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);
        return mapper.update(experimentEquipmentSign);
    }

    @Override
    @TaskAnnotation("updateByExperimentEquipmentId")
    public int updateByExperimentEquipmentId(SessionFactory factory, ExperimentEquipmentSignPo experimentEquipmentSign) {
        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);
        return mapper.updateByExperimentEquipmentId(experimentEquipmentSign);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public ExperimentEquipmentSignPo queryById(SessionFactory factory, String id) {

        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByExperimentEquipmentId")
    public List<ExperimentEquipmentSignPo> queryByExperimentEquipmentId(SessionFactory factory, String experimentEquipmentId) {
        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);

        return mapper.queryByExperimentEquipmentId(experimentEquipmentId);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ExperimentEquipmentSignPo> queryList(SessionFactory factory, ExperimentEquipmentSignPo experimentEquipmentSign) {

        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);

        List<ExperimentEquipmentSignPo> list = mapper.queryList(experimentEquipmentSign);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, ExperimentEquipmentSignPo experimentEquipmentSign, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        ExperimentEquipmentSignMapper mapper = factory.getMapper(ExperimentEquipmentSignMapper.class);

        List<ExperimentEquipmentSignPo> list = mapper.queryList(experimentEquipmentSign);

        return new PageInfo(list);
    }

}